package com.lagou.job;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
/**
 * @description: IntSortMapper
 * @author: yequn
 * @create: 2021-05-05 14:48
 **/
public class IntSortMapper extends Mapper<LongWritable, Text,IntWritable, NullWritable> {

    // 准备往reduce输出过去的key
    private IntWritable k = new IntWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 读取一行内容去掉前后的空字符串
        String numStr = value.toString().trim();
        // 剩下的内容如果还有值说明是个数字
        if(StringUtils.isNotEmpty(numStr)){
            k.set(Integer.parseInt(numStr));
            // 将k-v往下一级下，值可以忽略
            context.write(k,NullWritable.get());
        }
    }
}
